<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Http\Requests\RegisterRequest;
use App\Http\Requests\LoginRequest;

class AuthController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Registration & Login Controller
	|--------------------------------------------------------------------------
	|
	| This controller handles the registration of new users, as well as the
	| authentication of existing users. By default, this controller uses
	| a simple trait to add these behaviors. Why don't you explore it?
	|
	*/

	use AuthenticatesAndRegistersUsers;


    /**
     * @param Guard $auth
     * @param Registrar $registrar
     */
    public function __construct(Guard $auth, Registrar $registrar)
	{
		$this->auth = $auth;
		$this->registrar = $registrar;

		$this->middleware('guest', ['except' => 'getLogout']);
	}

    public function getRegister()
    {
        return View('auth.register');
    }

    public function postRegister(RegisterRequest $request)
    {
        $data = $request->except(['_token', 'conf_password']);

        $this->auth->login( $this->registrar->create($data) );

        return Redirect()->intended();
    }

    public function getLogin()
    {
        return View('auth.login');
    }


    public function postLogin(LoginRequest $request)
    {
        $data = $request->except('_token');

        if ($this->auth->attempt($data))
        {
            return redirect()->intended('/');
        }
        else
            return redirect()->back()->with('message', '用户名或密码不正确');
    }

    public function getLogout()
    {
        $this->auth->logout();

        return Redirect('/');
    }
}
